package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.icu.text.UFormat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Charging;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.CodeBitmapUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DensityUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.DrawableTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class CdbListAdapter extends RecyclerView.Adapter<CdbListAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private List<Charging.DataBean> mList;
    private String url;

    public CdbListAdapter(Context context,String url){
        mContext = context;
        this.url = url;
        mList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cdb_info,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.name.setText(mList.get(position).getName() == null ? "" :(String)mList.get(position).getName());
//        if (mList.get(position).getDump_energy().equals("100")) {
//            holder.electric.setText(100+"%");
//        }else {
//            holder.electric.setText(mList.get(position).getDump_energy().substring(1)+"%");
//        }
        int i = Integer.parseInt(mList.get(position).getDump_energy());


        holder.num.setText(position+1+"");
        holder.zhang.setText(mList.get(position).getSub_count()+" (今日)");
        if ( mList.get(position).getDevice_type() == 1) {
            //wiif设备
            holder.electric.setText("电量："+i+"%");
            holder.count.setText(mList.get(position).getTotal_charging_count()+"次");
            if (mList.get(position).getOn_line().equals("1")) {
                holder.icon.setImageResource(R.drawable.wifi);
               // holder.time.setDrawable(mContext.getResources().getDrawable(R.mipmap.shangxianshijian));
                holder.time.setText("上线时间：\n"+mList.get(position).getOnline_time());
            }else {
                //holder.time.setDrawable(mContext.getResources().getDrawable(R.mipmap.xiaxianshijian));
                holder.icon.setImageResource(R.drawable.wifitwo);
                holder.time.setText("离线时间：\n"+mList.get(position).getOffline_time());
            }
            holder.start.setVisibility(View.VISIBLE);
            if (mList.get(position).getGreen_light_status().equals("0")) {
                holder.start.setImageResource(R.mipmap.guanbi);
            }else if (mList.get(position).getGreen_light_status().equals("1")) {
                Glide.with(mContext).load(R.drawable.shanshuo).into(holder.start);
            }else {
                holder.start.setImageResource(R.mipmap.changliang);
            }
            String version = mList.get(position).getFirmware_version() ==null ? "" : (String)mList.get(position).getFirmware_version();
            holder.version.setText(mContext.getString(R.string.cdb_version)+version);
            holder.day.setText(mContext.getString(R.string.to_day)+mList.get(position).getTotal_online_count());
        }else if (mList.get(position).getDevice_type() == 4) {
            //密码设备

            holder.start.setVisibility(View.INVISIBLE);
            holder.icon.setImageResource(R.drawable.mimasuo);
           // holder.time.setDrawable(mContext.getResources().getDrawable(R.drawable.shijian));
            if (mList.get(position).getLast_use_time() == null || mList.get(position).getLast_use_time().equals("")) {
                holder.time.setText("上次使用:\n"+"未被使用");
            }else {
                holder.time.setText("上次使用:\n"+mList.get(position).getLast_use_time());
            }

            holder.version.setText("总电量：15000mA");
            if (mList.get(position).getToday_use_num() == null) {
                holder.electric.setText("今日充电：0");
                holder.day.setText("今日耗电量：0mA");
                holder.count.setText(mList.get(position).getTotal_charging_count()+"次");
            }else {
                holder.electric.setText("今日充电："+mList.get(position).getToday_use_num()+"次");
                if (mList.get(position).getToday_use_num().equals("0") || mList.get(position).getToday_use_num() == null) {
                    holder.day.setText("今日耗电量：0mA");
                }else {
                    holder.day.setText("今日耗电量："+mList.get(position).getToday_use_num()+"000mA");
                }
                int i1 = Integer.parseInt(mList.get(position).getTotal_charging_count()) + Integer.parseInt(mList.get(position).getToday_use_num());
                holder.count.setText(i1+"次");
            }
        }else if (mList.get(position).getDevice_type() == 5) {
            //纸巾机
            if (mList.get(position).getOn_line().equals("1")) {
                holder.icon.setImageResource(R.drawable.zjj);
            }else {
                holder.icon.setImageResource(R.drawable.zjjlx);
            }
            holder.start.setVisibility(View.GONE);

            holder.version.setText("剩余纸巾："+i);
            holder.electric.setText("今日领纸："+mList.get(position).getToday_use_num()+"次");
            holder.cdb_info_lj.setText("累计领纸：");
            int i1 = Integer.parseInt(mList.get(position).getTotal_charging_count()) + Integer.parseInt(mList.get(position).getToday_use_num());
            holder.count.setText(i1+"次");
            holder.day.setVisibility(View.GONE);
            if (mList.get(position).getLast_use_time() == null || mList.get(position).getLast_use_time().equals("")) {
                holder.time.setText("上次使用:\n"+"未被使用");
            }else {
                holder.time.setText("上次使用:\n"+mList.get(position).getLast_use_time());
            }
        }else{
            holder.day.setVisibility(View.VISIBLE);
            holder.cdb_info_lj.setText("历史累计充电：");
        }

        holder.page_er.setOnClickListener(v -> {
            Bitmap bitmap;
            if (mList.get(position).getDevice_type() == 4) {
                //密码设备
                if (mList.get(position).getPwd_type().equals("3")) {
                    bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.mima_url3)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
                }else if (mList.get(position).getPwd_type().equals("2")) {
                    bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.mima_url2)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
                }else{
                    bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.mima_url)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
                }
            }else {
                //wifi设备
                bitmap = CodeBitmapUtils.createImage(mContext.getString(R.string.wifi_url)+mList.get(position).getAlias(), DensityUtil.dip2px(mContext,200), DensityUtil.dip2px(mContext,200),null);
            }
            DialogUtils.TSImageDialog(mContext,(String)mList.get(position).getName(),mList.get(position).getAlias(),url,bitmap);
        });

        holder.number.setText(mContext.getString(R.string.facility_num)+mList.get(position).getAlias());

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(mList.get(position),position);
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        TextView num,number,name,day,version,count,electric,cdb_info_lj;
        ImageView icon,start,page_er;
        DrawableTextView time,zhang;
        public ViewHolder(View itemView) {
            super(itemView);
            time = (DrawableTextView) itemView.findViewById(R.id.cdb_info_time);
            zhang = (DrawableTextView) itemView.findViewById(R.id.cdb_info_zhang);
            num = (TextView) itemView.findViewById(R.id.cdb_info_num);
            number = (TextView) itemView.findViewById(R.id.cdb_info_number);
            day = (TextView) itemView.findViewById(R.id.cdb_info_day);
            name = (TextView) itemView.findViewById(R.id.cdb_info_name);
            version = (TextView) itemView.findViewById(R.id.cdb_info_version);
            count = (TextView) itemView.findViewById(R.id.cdb_info_count);
            electric = (TextView) itemView.findViewById(R.id.cdb_info_electric);
            cdb_info_lj = (TextView) itemView.findViewById(R.id.cdb_info_lj);
            icon = (ImageView) itemView.findViewById(R.id.cdb_info_icon);
            start = (ImageView) itemView.findViewById(R.id.cdb_info_start);
            page_er = (ImageView) itemView.findViewById(R.id.page_er);
        }
    }

    public void addData(List<Charging.DataBean> data){
        if (mList != null) {
            mList.clear();
            mList.addAll(data);
            notifyDataSetChanged();
        }
    }

    public interface OnItemClickListenter {
        void itemClickListenter(Charging.DataBean dataBean,int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }
}
